<script setup lang="ts">
import useEntStore from '@/store/modules/ent'
import type { JianCeRow } from '@/views/document/manager/create/create'

const { deviceId } = defineProps({
  deviceId: {
    type: String,
    default: '',
  },
})
const entStore = useEntStore()
const formData = ref<JianCeRow>({
  MN: '',
  PointCode: '',
  Code: '',
  Location: '',
  Name: '',
  ObjID: '',
  ParentId: '',
})
watchEffect(() => {
  const dataList = [...entStore.currentEntData.ElectricDevices, ...unref(entStore.Produces), ...unref(entStore.Reuses)]
  // 递归查询ObjID相等的数据
  function findObj(data: any, id: string): JianCeRow | undefined {
    for (let i = 0; i < data.length; i++) {
      if (data[i].ObjID === id) {
        return data[i]
      }
      if (data[i].Children) {
        const res = findObj(data[i].Children, id)
        if (res) {
          return res
        }
      }
    }
  }
  const data = findObj(dataList, deviceId) as JianCeRow
  formData.value = data || {
    MN: '',
    PointCode: '',
    Code: '',
    Location: '',
    Name: '',
    ObjID: '',
    ParentId: '',
  }
})
</script>

<template>
  <div>
    <el-form :model="formData" label-width="120px" label-suffix="：">
      <el-row>
        <el-col :span="12">
          <el-form-item label="MN号">
            <el-input v-model="formData.MN" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="采集因子">
            <el-input v-model="formData.PointCode" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="设备编码">
            <el-input v-model="formData.Code" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="安装位置">
            <el-input v-model="formData.Location" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>
